package com.example.onion.hxchat.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.databean.FriendBean;
import com.example.onion.hxchat.activity.FriendApplyNotificationActivity;
import com.example.onion.hxchat.activity.ScanUserActivity;
import com.example.onion.hxchat.adapter.MyFriendAdapter;
import com.example.onion.hxchat.adapter.MyTeamNotificationAdapter;
import com.example.onion.hxchat.mvvm.ApplyViewModel;
import com.example.onion.hxchat.mvvm.TeamNotificationViewModel;
import com.example.onion.hxchat.mvvm.WcFriendViewModel;
import com.example.onion.hxchat.room.entity.Apply;
import com.example.onion.hxchat.room.entity.Friend;
import com.example.onion.hxchat.room.entity.TeamNotification;
import com.example.onion.hxchat.util.MyLocalDatabaseHelper;
import com.example.onion.hxchat.util.PassWordHelp;
import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:onion
 * @data: 2020/8/21
 */
public class TeamNotificationFragment  extends Fragment {

    private View view;  //定义view用来设置fragment的layout
    public RecyclerView mRecyclerView;  //定义RecyclerView

    private List<TeamNotification> mList = new ArrayList<>();

    private MyTeamNotificationAdapter myTeamNotificationAdapter;

    private TeamNotificationViewModel teamNotificationViewModel;
    private String accountId;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_team_notification, container, false);
        initRecyclerView();
        accountId = PassWordHelp.readPassword(requireContext())[0];
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        teamNotificationViewModel = new ViewModelProvider(requireActivity()).get(TeamNotificationViewModel.class);
        teamNotificationViewModel.getOwnerIdTeamNotification(accountId).observe(requireActivity(), new Observer<List<TeamNotification>>() {
            @Override
            public void onChanged(List<TeamNotification> teamNotifications) {
                myTeamNotificationAdapter.setmList(teamNotifications);
                myTeamNotificationAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    /**
     * TODO 对recycleview进行配置
     */

    private void initRecyclerView() {

        //获取RecyclerView
        mRecyclerView = view.findViewById(R.id.team_notification_fragment_recyclerview_wc);
        //创建adapter
        myTeamNotificationAdapter = new MyTeamNotificationAdapter(getContext());
        //适配器只负责得到数据吧，跳转页面由从适配器获取到数据的Activity或者Fragment（本fragment页面）去选择跳转到哪个页面
        myTeamNotificationAdapter.setDataGetterListener(new MyTeamNotificationAdapter.DataGetterListener() {
            @Override
            public void getItemData(TeamNotification teamNotification) {//teamNotification通过回调机制从适配器中得到的teamNotification数据
                //
                teamNotificationViewModel.deleteTeamNotification(teamNotification);
            }

        });
        //给RecyclerView设置adapter
        mRecyclerView.setAdapter(myTeamNotificationAdapter);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        //设置item的分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
//        myFriendAdapter.setOnItemClickListener(new CollectRecycleAdapter.OnItemClickListener() {
//            @Override
//            public void OnItemClick(View view, GoodsEntity data) {
//                //此处进行监听事件的业务处理
//                Toast.makeText(getActivity(),"我是item",Toast.LENGTH_SHORT).show();
//            }
//        });
    }

}
